<?php

class Model_Products_Detail extends Zend_Db_Table_Abstract {

	protected $_name = 'products_v1_detail';
	
	public function getDetail( $idProduct){
		
		$row = $this->fetchRow("`id_product` = ". $idProduct);
		if(!$row) return false;
		return $row->toArray();		
		
		}
	public function getDetailByCode($codeProduct){
		
		if(trim($codeProduct)!=""){
			
			$row = $this->fetchRow("`code_product` LIKE '{$codeProduct}' ");
			if(!$row) return false;
			return $row->toArray();			
		}
		
		return false;
	}	
	
	public function getDetailByShortCode($shortCode){

		if(trim($shortCode)!=""){		
			$row = $this->fetchRow("`code_product` LIKE '{$shortCode}%' ");
			if(!$row) return false;
			return $row->toArray();
		}
		
		return false;
		
	}
	/**
	 * @auther Norman
	 * @version 2014-813
	 * Change to suit Multiple website and multiple price 
	 * @param unknown_type $idProduct
	 * @param unknown_type $codeProduct
	 * @param unknown_type $aposTitle
	 * @param unknown_type $mobileBrand
	 * @param unknown_type $mobileModel
	 * @param unknown_type $category
	 * @param unknown_type $shortDescriptionProduct
	 * @param unknown_type $chineseTitleProduct
	 * @param unknown_type $descriptionProduct
	 * @param unknown_type $webpageDescription
	 * @param unknown_type $feature
	 * @param unknown_type $sizeMm
	 * @param unknown_type $weightKg
	 * @param unknown_type $introduced
	 * @param unknown_type $imgUrl
	 * @param unknown_type $imgIndiUrl
	 * @param unknown_type $imgZip
	 * @param unknown_type $nameSupplier
	 * @param unknown_type $barcodeSupplier
	 * @param unknown_type $ebayCategory
	 * @param unknown_type $postage
	 */	
	public function addDetail( $idProduct ,$codeProduct ,$aposTitle ,$mobileBrand , $mobileModel , $category , $shortDescriptionProduct , $chineseTitleProduct , $descriptionProduct , $webpageDescription , $feature , $sizeMm , $weightKg , $introduced , $imgUrl , $imgIndiUrl,$imgZip , $nameSupplier , $barcodeSupplier,$ebayCategory,$postage,$color){
		
		$data = array(		
		 "id_product" => $idProduct,					
         "code_product" =>  $codeProduct ,
		 "apos_title_product" => $aposTitle,		
         "mobile_brand" =>  $mobileBrand ,
         "mobile_model" =>  $mobileModel ,
         "category" =>  $category ,
		 "ebay_category" =>$ebayCategory,
         "short_description_product" =>  $shortDescriptionProduct ,
         "chinese_title_product" =>  $chineseTitleProduct ,
         "description_product" =>  $descriptionProduct ,
         "webpage_description" =>  $webpageDescription ,
         "feature" =>  $feature ,
         "size_mm" =>  $sizeMm ,
         "weight_kg" =>  $weightKg ,
		 "postage_price" => $postage,
         "introduced" =>  $introduced ,
         "img_url" =>  $imgUrl ,
		 "img_individual_url" =>  $imgIndiUrl ,
         "img_zip" =>  $imgZip ,
         "name_supplier" =>  $nameSupplier ,
         "barcode_supplier" =>  $barcodeSupplier,
		 "color"  => $color		
			);
		$this->insert($data);
		}
		//20140530
		public function addProduct($idProduct,$code_product,$chinese_title_product,$apos_title_product,$first_import,$fitamount_import,$mobile_brand,$mobile_model,$category,$ebay_title,$webpage_description,$description_product,$size_mm,$weight_kg,$img_url,$img_individual_url,$img_individual_url,$img_zip,$barcode_supplier,$name_supplier,$code_supplier,$post_fee,$color,$multy_price,$Location){
		
			$data = array(
					"id_Product"=>$idProduct,
					"code_product"=>$code_product,
					"chinese_title_product"=>$chinese_title_product,
					"apos_title_product"=>$apos_title_product,
					"first_import"=>$first_import,
					"fitamount_import"=>$fitamount_import,
					"mobile_brand"=>$mobile_brand,
					"mobile_model"=>$mobile_model,
					"category"=>$category,
					"ebay_title"=>$ebay_title,
					"webpage_description"=>$webpage_description,
					"description_product"=>$description_product,
					"size_mm"=>$size_mm,
					"weight_kg"=>$weight_kg,
					"img_url"=>$img_url,
					"img_individual_url"=>$img_individual_url,
					"img_individual_url"=>$img_individual_url,
					"img_zip"=>$img_zip,
					"barcode_supplier"=>$barcode_supplier,
					"name_supplier"=>$name_supplier,
					"code_supplier"=>$code_supplier,
					"post_fee"=>$post_fee,
					"color"=>$color,
					"multy_price"=>$multy_price,
					"Location"=>$Location
			);
			$this->insert($data);
		}
		
	public function updateDetail(  $idProduct ,$codeProduct ,$aposTitle ,$mobileBrand , $mobileModel , $category , $shortDescriptionProduct , $chineseTitleProduct , $descriptionProduct , $webpageDescription , $feature , $sizeMm , $weightKg , $introduced , $imgUrl , $imgIndiUrl,$imgZip , $nameSupplier , $barcodeSupplier,$ebayCategory,$postage,$color){
		
		$data = array(
         "code_product" =>  $codeProduct ,
		 "apos_title_product" => $aposTitle,		
         "mobile_brand" =>  $mobileBrand ,
         "mobile_model" =>  $mobileModel ,
         "category" =>  $category ,
         "short_description_product" =>  $shortDescriptionProduct ,
         "chinese_title_product" =>  $chineseTitleProduct ,
         "description_product" =>  $descriptionProduct ,
         "webpage_description" =>  $webpageDescription ,
         "feature" =>  $feature ,
         "size_mm" =>  $sizeMm ,
         "weight_kg" =>  $weightKg ,
         "introduced" =>  $introduced ,
         "img_url" =>  $imgUrl ,
		 "img_individual_url" =>  $imgIndiUrl ,
         "img_zip" =>  $imgZip ,
         "name_supplier" =>  $nameSupplier ,
         "barcode_supplier" =>  $barcodeSupplier,
		 "ebay_category" =>$ebayCategory,
		 "postage_price" => $postage,
		 "color" => $color		
		);
			
		$this->update($data,"`id_product` = ". $idProduct);
		}
		
	public function deleteDetail( $idProduct){
		
		$this->delete("`id_product` = ". $idProduct);
		
		}	
	
	public function listAll(){
		
		$rows =$this->fetchAll("1","id_product DESC ");
		if(!$rows) return false;
		return $rows->toArray();			
		
		}	
	public function importDetail($idProduct,$codeProduct,$des){
		
		if(!self::getDetail($idProduct)){
		$data = array(
				"id_product" => $idProduct,
				"code_product" =>  $codeProduct ,
				"description_product" =>  $des
		);
		$this->insert($data);		
		}
	}
	
	public function getProductsBymanuBymodel($mobileBrand , $mobileModel){
		
		$whereStr = "`mobile_brand` LIKE '{$mobileBrand}' AND `mobile_model` LIKE '{$mobileModel}'";
		$rows = $this->fetchAll($whereStr,"code_product")->toArray();
		if(empty($rows)) {return false;}
		return $rows;
		
	}
	/**
	 * @author Norman
	 * @version 2014-08-14
	 * 
	 * @param Date $dateUpdate
	 */
 	public function listUpdatedByDate($dateUpdate){
 		
 		$whereStr = " `introduced` = '{$dateUpdate}' ";
 		$rows = $this->fetchAll($whereStr,"code_product");
 		if(!(bool)$rows->toArray()) {
 			return false;
 		}
 		return $rows->toArray();
 	}	
 	/**
 	 * add on 2014-11-15 update image 
 	 */
	public function updateSingleImg($idProduct , $imgIndiUrl){
		
		$data = array(
				"img_individual_url" =>  $imgIndiUrl
		);
			
		$this->update($data,"`id_product` = ". $idProduct);
	}
	public function updateImg($idProduct,$imgUrl){
		
		$data = array(
				"img_url" =>  $imgUrl
		);
			
		$this->update($data,"`id_product` = ". $idProduct);
		
	}
	public function lastUpdate($codeProduct,$dateUpdate = null){
		
		$dateToUse = Model_DatetimeHelper::dateToday();
		if($dateUpdate !=""){
			$dateToUse = $dateUpdate;
		}
		
		$data = array(
				"last_update" => $dateToUse
				);
		
		$this->update($data,"`code_product` LIKE '". $codeProduct."'");
		
	}
	
	public function listLastUpdate($dateUpdate){
		
		
		$whereStr = " `last_update` = '{$dateUpdate}' ";
		$rows = $this->fetchAll($whereStr,"code_product");
		if(!(bool)$rows->toArray()) {
			return false;
		}
		return $rows->toArray();
		
		
	}
}




?>